//
//  FLMMessageController.m
//  FLMSuperS
//
//  Created by xiaoyang on 16/9/5.
//  Copyright © 2016年 FaLaoMiaoNetworkTechnologyCo. All rights reserved.
//

#import "FLMMessageController.h"
#import "FLMSearchBar.h"
#import "FLMMessageTableViewCell.h"
#import "FLMChatViewController.h"
#import "FLMAssistantViewController.h"

static NSString *const kFLMMessageTableViewCellIdentifier = @"FLMMessageTableViewCell";

@interface FLMMessageController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *messages;

@end

@implementation FLMMessageController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    [self setUpNavContent];
    _messages = @[@{
                      @"title" :@"超级买消息中心",
                      @"info" :@"超级买消息、留言客户",
                      @"time" : @"12:59"
                      },
                  @{
                      @"title" :@"超级买助手",
                      @"info" :@"超级买教程、问题解答",
                      @"time" : @"12:59"
                      }].mutableCopy;
    [self.view addSubview:self.tableView];
}

- (void)setUpNavContent {
    FLMSearchBar *searchBar = [FLMSearchBar searchBar];
    searchBar.backgroundColor = [UIColor whiteColor];
    searchBar.delegate = self;
    searchBar.placeholder = @"搜索";
    searchBar.left = 20;
    searchBar.width = self.view.width - 40;
    searchBar.height = 30;
    searchBar.centerY = self.navigationController.navigationBar.height/2;
    
    self.navigationController.navigationBar.backgroundColor = [UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0];

    UIBarButtonItem *searchButton = [[UIBarButtonItem alloc]initWithCustomView:searchBar];
    self.navigationItem.rightBarButtonItems = [NSArray arrayWithObject:searchButton];
}

#pragma mark - UITextField
- (void)textFieldDidEndEditing:(UITextField *)textField {
    
}

#pragma mark - UITableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _messages.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FLMMessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kFLMMessageTableViewCellIdentifier forIndexPath:indexPath];
    cell.model = _messages[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.row == 0) {
        FLMChatViewController *chatVc = [[FLMChatViewController alloc] init];
        
        //[chatVc setHidesBottomBarWhenPushed:YES];
        
        [self.navigationController pushViewController:chatVc animated:YES];
    }
    else {
        FLMAssistantViewController *assistantVc = [[FLMAssistantViewController alloc] init];
        
        //[assistantVc setHidesBottomBarWhenPushed:YES];
        
        [self.navigationController pushViewController:assistantVc animated:YES];
    }
}

- (UITableView *)tableView {
    if(!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH - 49- 0)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = 90;
        [_tableView registerClass:[FLMMessageTableViewCell class] forCellReuseIdentifier:kFLMMessageTableViewCellIdentifier];
    }
    return _tableView;
}

@end
